<?php
/**
 *      [Jsen.net] (C)2013-2099 Jason Group Science and technology co., LTD.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      http://www.jsen.net
 *      tel:400-681-9511
 */
 
 //<form action="https://newpay.ips.com.cn/psfp-entry/gateway/payment.do" method="post">
libfile('pay_abstract');
class ips extends pay_abstract {
	public function __construct($config = array()) {	
		if (!empty($config)) $this->set_config($config);        
		$this->config['gateway_url'] = 'https://newpay.ips.com.cn/psfp-entry/gateway/payment.do';
		$this->config['gateway_method'] = 'POST';
		$this->config['notify_url'] = return_url('ips', 'notify');
		$this->config['return_url'] = return_url('ips', 'return');
	}

	public function getpreparedata() {
		
		$prepare_data['Version'] = "v1.0.0";
		$prepare_data['MerBillNo'] = $this->product_info['trade_sn'];
		$prepare_data['GatewayType'] = "01";
		$prepare_data['Date'] =date("Ymd");
		$prepare_data['CurrencyType'] = 156;
		$prepare_data['Amount'] =$this->product_info['total_fee'];
		$prepare_data['Lang'] = "GB";
		$prepare_data['Merchanturl'] = $this->config['return_url'];
		$prepare_data['FailUrl'] = "";
		$prepare_data['Attach'] = "";
		$prepare_data['OrderEncodeType'] = 5;
		$prepare_data['RetEncodeType'] = 17;
		$prepare_data['RetType'] = 1;
		$prepare_data['ServerUrl'] = $this->config['notify_url'];
		$prepare_data['BillEXP'] = "";
		$prepare_data['GoodsName'] = "账户充值";
		$prepare_data['IsCredit'] = "";
		$prepare_data['BankCode'] = "";
		$prepare_data['ProductType'] = "";
		
		//请求报文的消息体
		  $strbodyxml= "<body>"
					 ."<MerBillNo>".$prepare_data["MerBillNo"]."</MerBillNo>"
					 ."<Amount>".$prepare_data["Amount"]."</Amount>"
					 ."<Date>".$prepare_data["Date"]."</Date>"
					 ."<CurrencyType>".$prepare_data["CurrencyType"]."</CurrencyType>"
					 ."<GatewayType>".$prepare_data["GatewayType"]."</GatewayType>"
						 ."<Lang>".$prepare_data["Lang"]."</Lang>"
					 ."<Merchanturl>".$prepare_data["Merchanturl"]."</Merchanturl>"
					 ."<FailUrl>".$prepare_data["FailUrl"]."</FailUrl>"
						 ."<Attach>".$prepare_data["Attach"]."</Attach>"
						 ."<OrderEncodeType>".$prepare_data["OrderEncodeType"]."</OrderEncodeType>"
						 ."<RetEncodeType>".$prepare_data["RetEncodeType"]."</RetEncodeType>"
						 ."<RetType>".$prepare_data["RetType"]."</RetType>"
						 ."<ServerUrl>".$prepare_data["ServerUrl"]."</ServerUrl>"
						 ."<BillEXP>".$prepare_data["BillEXP"]."</BillEXP>"
						 ."<GoodsName>".$prepare_data["GoodsName"]."</GoodsName>"
						 ."<IsCredit>".$prepare_data["IsCredit"]."</IsCredit>"
						 ."<BankCode>".$prepare_data["BankCode"]."</BankCode>"
						 ."<ProductType>".$prepare_data["ProductType"]."</ProductType>"
				  ."</body>";

	  $Sign=$strbodyxml.$pMerCode.$pMerCert;//签名明文
	  
	  $pSignature = md5($strbodyxml.$this->config["account"].$this->config["MD5"]);//数字签名 
	  //请求报文的消息头
	  $strheaderxml= "<head>"
					   ."<Version>".$prepare_data["Version"]."</Version>"
					   ."<MerCode>".$this->config["account"]."</MerCode>"
					   ."<MerName>".$this->config["accountname"]."</MerName>"
					   ."<Account>".$this->config["partner"]."</Account>"
					   ."<MsgId>".$prepare_data["MsgId"]."</MsgId>"
					   ."<ReqDate>".date("YmdHis")."</ReqDate>"
					   ."<Signature>".$pSignature."</Signature>"
				  ."</head>";
	 
	//提交给网关的报文
	$strsubmitxml =  "<Ips>"
				  ."<GateWayReq>"
				  .$strheaderxml
				  .$strbodyxml
			  ."</GateWayReq>"
				."</Ips>";
		
		echo '<form name="form1" id="form1" method="post" action="'.$this->config['gateway_url'].'"><input type="hidden" name="pGateWayReq" value="'.$strsubmitxml.'" /></form><script language="javascript">document.form1.submit();</script>';
		exit;
	}

	public function _delivery() {
		return TRUE;
	}
    
    public function _return() {
	   $paymentResult = $_POST["paymentResult"];
	   $xml=simplexml_load_string($paymentResult,'SimpleXMLElement', LIBXML_NOCDATA); 
		 //读取相关xml中信息
	   $ReferenceIDs = $xml->xpath("GateWayRsp/head/ReferenceID");//关联号
	   //var_dump($ReferenceIDs); 
	   $ReferenceID = $ReferenceIDs[0];//关联号
	   $RspCodes = $xml->xpath("GateWayRsp/head/RspCode");//响应编码
	   $RspCode=$RspCodes[0];
	   $RspMsgs = $xml->xpath("GateWayRsp/head/RspMsg"); //响应说明
	   $RspMsg=$RspMsgs[0];
	   $ReqDates = $xml->xpath("GateWayRsp/head/ReqDate"); // 接受时间
		$ReqDate=$ReqDates[0];
	   $RspDates = $xml->xpath("GateWayRsp/head/RspDate");// 响应时间
		$RspDate=$RspDates[0];
	   $Signatures = $xml->xpath("GateWayRsp/head/Signature"); //数字签名
		$Signature=$Signatures[0];
	   $MerBillNos = $xml->xpath("GateWayRsp/body/MerBillNo"); // 商户订单号
		$MerBillNo=$MerBillNos[0];
	   $CurrencyTypes = $xml->xpath("GateWayRsp/body/CurrencyType");//币种
		$CurrencyType=$CurrencyTypes[0];
	   $Amounts = $xml->xpath("GateWayRsp/body/Amount"); //订单金额
		$Amount=$Amounts[0];
	   $Dates = $xml->xpath("GateWayRsp/body/Date");    //订单日期
		$Date=$Dates[0];
	   $Statuss = $xml->xpath("GateWayRsp/body/Status");  //交易状态
		$Status=$Statuss[0];
	   $Msgs = $xml->xpath("GateWayRsp/body/Msg");    //发卡行返回信息
		$Msg=$Msgs[0];
	   $Attachs = $xml->xpath("GateWayRsp/body/Attach");    //数据包
		$Attach=$Attachs[0];
	   $IpsBillNos = $xml->xpath("GateWayRsp/body/IpsBillNo"); //IPS订单号
		$IpsBillNo=$IpsBillNos[0];
	   $IpsTradeNos = $xml->xpath("GateWayRsp/body/IpsTradeNo"); //IPS交易流水号
		$IpsTradeNo=$IpsTradeNos[0];
	   $RetEncodeTypes = $xml->xpath("GateWayRsp/body/RetEncodeType");    //交易返回方式
		$RetEncodeType=$RetEncodeTypes[0];
	   $BankBillNos = $xml->xpath("GateWayRsp/body/BankBillNo"); //银行订单号
		$BankBillNo=$BankBillNos[0];
	   $ResultTypes = $xml->xpath("GateWayRsp/body/ResultType"); //支付返回方式
		$ResultType=$ResultTypes[0];
	   $IpsBillTimes = $xml->xpath("GateWayRsp/body/IpsBillTime"); //IPS处理时间
		$IpsBillTime=$IpsBillTimes[0];
		
		 if($Attach!=""){
		 	$str= "<Attach><![CDATA[" . $Attach . "]]></Attach>";
		 }else{
			$str="";	 
		 }
		 $sbReq = "<body>"
                          . "<MerBillNo>" . $MerBillNo . "</MerBillNo>"
                          . "<CurrencyType>" . $CurrencyType . "</CurrencyType>"
                          . "<Amount>" . $Amount . "</Amount>"
                          . "<Date>" . $Date . "</Date>"
                          . "<Status>" . $Status . "</Status>"
                          . "<Msg><![CDATA[" . $Msg . "]]></Msg>"
                          . $str
                          . "<IpsBillNo>" . $IpsBillNo . "</IpsBillNo>"
                          . "<IpsTradeNo>" . $IpsTradeNo . "</IpsTradeNo>"
                          . "<RetEncodeType>" . $RetEncodeType . "</RetEncodeType>"
                          . "<BankBillNo>" . $BankBillNo . "</BankBillNo>"
                          . "<ResultType>" . $ResultType . "</ResultType>"
                          . "<IpsBillTime>" . $IpsBillTime . "</IpsBillTime>"
                       . "</body>";
					 
		$sign=$sbReq.$this->config['account'].$this->config['MD5'];
		$md5sign=  md5($sign);
		//订单号
		$BillNo = $IpsBillNo;
		//金额
		$MerBillNo=json_decode(json_encode($MerBillNo),true);
		$IpsBillNo=json_decode(json_encode($IpsBillNo),true);
		$Amount = $Amount;
		//$_REQUEST["BillNo"]="cz2015111617395809503";
        if ($md5sign==$Signature){
			if($RspCode=='000000')
    		{		
				$result['result'] = 'success';
				$result['pay_code'] = 'ips';
				$result['trade_no'] = $IpsBillNo[0];
				$result['out_trade_no'] = $MerBillNo[0];
				return $result;
			}else{
				return false;	
			}
		} else {
            return false;
        }
    }
    /**
	 * POST接收数据
	 * 状态码说明  （0 交易完成 1 待付款 2 待发货 3 待收货 4 交易关闭 5交易取消
	 */
    public function _notify() {
        return $this->_return();
    }
	
    /**
     * 相应服务器应答状态
     * @param $result
     */
    public function response($result) {
    	if (FALSE == $result) echo '';
		else echo 'success';
    }
    
    /**
     * 返回字符过滤
     * @param $parameter
     */
    private function filterParameter($parameter)
    {
        $para = array();
        foreach ($parameter as $key => $value) {
            if ('sign' == $key || 'sign_type' == $key || '' == $value || 'm' == $key  || 'a' == $key  || 'c' == $key   || 'code' == $key || 'method' == $key || 'page' == $key) continue;
            else $para[$key] = $value;
        }
        return $para;
    }
    public function getCodeUrl(){
    	
    }
}